package com.dayuanit.dy15.atm.config;

/**
 * @author echo
 * @date 2020/5/23 - 12:10
 */
import com.google.common.base.Predicates;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author echo
 * @date 2020/4/29 - 16:51
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {


    @Bean
    public Docket webApiConfig(Environment environment) {
        // 设置要显示swagger的环境
        Profiles of = Profiles.of("dev", "test");
        // 判断当前是否处于该环境
        // 通过 enable() 接收此参数判断是否要显示
        //environment.acceptsProfiles 获取当前激活环境
        boolean b = environment.acceptsProfiles(of);

        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("webApi")
//                .enable(b)
                .apiInfo(webApiInfo())
                .select()
                .paths(Predicates.not(PathSelectors.regex("/admin/.*")))
                .paths(Predicates.not(PathSelectors.regex("/error.*")))
                .build();
    }

    private ApiInfo webApiInfo() {
        //作者信息
        // Contact contact = new Contact("echo","http://test.com","1848616572@qq.com");
        return new ApiInfoBuilder()
                .title("Swagger测试文档")
                .description("ATM接口")
                .version("1.0")
                //Contact 作者信息
                .contact(new Contact("java", "http://test.com", "1848616572@qq.com"))
                .build();
    }

}
